﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace UnityEngine.Terrain
{
    public class WCLoader
    {
        public WCLoader(string Name, System.IO.Stream Stream)
        {
            _Name = Name;

            Reader = new System.IO.BinaryReader(Stream, Encoding.ASCII);
            LocationStack = new System.Collections.Generic.LinkedList<int>();
        }

        public bool Eof()
        {
            return (Reader.BaseStream.Position >= Reader.BaseStream.Length);
        }

        public byte[] Read(int Size)
        {
            return Reader.ReadBytes(Size);
        }

        public byte ReadByte()
        {
            return Reader.ReadByte();
        }

        public int ReadInt8()
        {
            return (int)Reader.ReadByte();
        }

        public int ReadInt16()
        {
            return (int)Reader.ReadInt16();
        }

        public int ReadInt32()
        {
            return Reader.ReadInt32();
        }

        public float ReadFloat()
        {
            return Reader.ReadSingle();
        }

        public double ReadDouble()
        {
            return Reader.ReadDouble();
        }

        public string ReadString(int Length)
        {
            int BufferLength = Length;
            char[] Buffer = Reader.ReadChars(Length);

            while (BufferLength > 0)
            {
                if (Buffer[BufferLength - 1] != '\0') break;
                BufferLength--;
            }

            return new string(Buffer, 0, BufferLength);
        }

        public Vector3 ReaderVector3()
        {
            Vector3 ret = new Vector3();
            ret.X = this.ReadFloat();
            ret.Y = this.ReadFloat();
            ret.Z = this.ReadFloat();
            return ret;
        }

        public string ReadTag()
        {
            return ReadString(4);
        }

        public void ExpectTag(string ExpectedTag)
        {
            string Tag = ReadTag();
            if (Tag != ExpectedTag)
            {
                throw new System.Exception("Error at location " + Location + ", expected \"" + ExpectedTag + "\", got \"" + Tag + "\"!");
            }
        }

        public void Skip(int NrOfBytes)
        {
            Reader.ReadBytes(NrOfBytes);
        }

        public void PushLocation()
        {
            LocationStack.AddLast((int)Reader.BaseStream.Position);
        }

        public int PopLocation()
        {
            int Location = LocationStack.Last.Value;
            LocationStack.RemoveLast();
            return (int)Reader.BaseStream.Position - Location;
        }

        public long Location
        {
            get
            {
                return Reader.BaseStream.Position;
            }
        }

        public string Name
        {
            get
            {
                return _Name;
            }
        }

        private string _Name = "";

        private System.IO.BinaryReader Reader = null;
        private System.Collections.Generic.LinkedList<int> LocationStack = null;
    }
}
